package com.zlll.winner.controller.tencentcloud;

import com.alibaba.fastjson.JSONObject;
import com.zlll.winner.business.model.media.MediaNewList;
import com.zlll.winner.business.service.media.IMediaNewListService;
import com.zlll.winner.enums.media.MediaEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping(value = "/cloud/message")
@Slf4j
public class TencentCloudCallbackController {


    @Reference
    private IMediaNewListService mediaNewListService;

    /**
     * 录制会掉函数
     * @param params
     * @return
     */
    @PostMapping(value = "recordNotify")
    public Object recordNotify(@RequestBody JSONObject params) {
        log.error("recordNotify=====>{}", params.toJSONString());
        JSONObject result = new JSONObject();
        Integer eventType = params.getInteger("event_type");
        if (eventType.equals(100)) { //直播录制回调
            Long taskId = params.getLong("task_id");
            String videoUrl = params.getString("video_url");
            String fileId = params.getString("file_id");
            if (taskId != null) {
                MediaNewList updateObj = mediaNewListService.getMediaNewListByTaskId(taskId);
                if(updateObj.getReplayUrl()!=null){
                    updateObj.setReplayUrl(updateObj.getReplayUrl()+","+videoUrl);
                    updateObj.setReplayFileId(updateObj.getReplayFileId()+","+fileId);
                }else{
                    updateObj.setReplayUrl(videoUrl);
                    updateObj.setReplayFileId(fileId);
                }
                updateObj.setStatus(MediaEnum.StatusEnum.REPLAY.val());
                boolean b = mediaNewListService.updateById(updateObj);
                mediaNewListService.livingMediaNewList();
            }
        }
        result.put("code", 0);
        return result;
    }
}
